<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="行程状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择行程状态" clearable>
          <el-option v-for="dict in dict.type.booking_status" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="预约类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择预约类型" clearable>
          <el-option v-for="dict in dict.type.booking_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="用车时间">
        <el-date-picker v-model="daterangeArrivalTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="车站名称" prop="stationId">
        <el-select v-model="queryParams.stationId" placeholder="请选择车站名称" clearable>
          <el-option v-for="station in stationList" :key="station.id" :label="station.stationName"
            :value="station.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="乘客姓名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入乘客姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="房间编号" prop="roomNo">
        <el-input v-model="queryParams.roomNo" placeholder="请输入房间编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input v-model="queryParams.driverName" placeholder="请输入司机姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTodayQuery">今日行程</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleTomorrowQuery">明日行程</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['hotel:booking:add']">新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hotel:booking:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['hotel:booking:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['hotel:booking:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="bookingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="行程编号" align="center" prop="id" width="80" fixed="left" />
      <el-table-column label="行程状态" align="center" prop="status" width="80" fixed="left">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.booking_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="预约类型" align="center" prop="type" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.booking_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="用车时间" align="center" prop="arrivalTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车站名称" align="center" prop="stationName" width="80" />
      <el-table-column label="房间编号" align="center" prop="roomNo" width="80" />
      <el-table-column label="入住时间" align="center" prop="bookingTIme" width="80" />
      <el-table-column label="乘客姓名" align="center" prop="username" width="100" />
      <el-table-column label="联系方式" align="center" prop="phone" width="120" />
      <el-table-column label="微信昵称" align="center" prop="wxUserName" width="120" />
      <el-table-column label="微信手机号" align="center" prop="userPhone" width="120" />
      <el-table-column label="出行人数" align="center" prop="personCount" width="80" />
      <el-table-column label="备注" align="center" prop="remark" width="200" />
      <el-table-column label="安排车型" align="center" prop="carDesc" width="100" />
      <el-table-column label="司机姓名" align="center" prop="driverName" width="100" />
      <el-table-column label="司机联系方式" align="center" prop="driverPhone" width="120" />
      <el-table-column label="确认时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出发时间" align="center" prop="departureTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="completeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="取消时间" align="center" prop="cancelTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cancelTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            :disabled="scope.row.status == 2 || scope.row.status == 3"
            v-hasPermi="['hotel:booking:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['hotel:booking:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改行程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="行程状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in dict.type.booking_status" :key="dict.value" :label="dict.value" :disabled="dict.value == 1">
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预约类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择预约类型" disabled>
                <el-option v-for="dict in dict.type.booking_type" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="车站名称" prop="stationId">
              <el-select v-model="form.stationId" placeholder="请选择车站名称" clearable disabled>
                <el-option v-for="station in stationList" :key="station.id" :label="station.stationName"
                  :value="station.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用车时间" prop="arrivalTime" >
              <el-date-picker clearable v-model="form.arrivalTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择用车时间" disabled>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12" v-if="form.type === '2'">
            <el-form-item label="房间编号" prop="roomNo">
              <el-input v-model="form.roomNo" placeholder="请输入房间编号" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.type === '1'">
            <el-form-item label="入住时间" prop="roomNo">
              <el-input v-model="form.bookingTime" placeholder="请输入入住时间" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出行人数" prop="personCount">
              <el-input v-model="form.personCount" placeholder="请输入出行人数" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="乘客姓名" prop="username">
              <el-input v-model="form.username" placeholder="请输入乘客姓名" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系方式" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系方式" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="微信昵称" prop="wxUserName">
              <el-input v-model="form.wxUserName" placeholder="请输入微信昵称" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="微信手机号" prop="userPhone">
              <el-input v-model="form.userPhone" placeholder="请输入微信手机号" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="安排车型" prop="carDesc">
              <el-input v-model="form.carDesc" placeholder="请输入安排车型" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="司机姓名" prop="driverName">
              <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机手机" prop="driverPhone">
              <el-input v-model="form.driverPhone" placeholder="请输入司机联系方式" />
            </el-form-item>
          </el-col>
           <el-col :span="12" v-if="form.status == 4">
            <el-form-item label="确认时间" prop="confirmTime">
              <el-date-picker clearable v-model="form.confirmTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择确认时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12"  v-if="form.status == 5">
            <el-form-item label="出发时间" prop="departureTime">
              <el-date-picker clearable v-model="form.departureTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择出发时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12"  v-if="form.status == 2">
            <el-form-item label="完成时间" prop="completeTime">
              <el-date-picker clearable v-model="form.completeTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择完成时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBooking, getBooking, delBooking, addBooking, updateBooking } from "@/api/hotel/booking";
import { listStation } from "@/api/hotel/station";
export default {
  name: "Booking",
  dicts: ['booking_type', 'booking_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 车站列表
      stationList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行程表格数据
      bookingList: [],
      // 出发时间时间范围
      daterangeArrivalTime: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        type: null,
        bookingTime: null,
        arrivalTime: null,
        stationId: null,
        phone: null,
        userPhone: null,
        personCount: null,
        stationName: null,
        username: null,
        roomNo: null,
        carDesc: null,
        driverName: null,
        driverPhone: null,
        confirmTime: null,
        departureTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        carDesc: [
          { required: true, message: "请填写车型描述", trigger: "blur" }
        ],
        driverName: [
          { required: true, message: "请填写司机姓名", trigger: "blur" }
        ],
        driverPhone: [
          { required: true, message: "请填写司机电话", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getStationList();

    // 获取路由参数
    const { type } = this.$route.query;
    if (type === 'today') {
      this.handleTodayQuery();
    } else if (type === 'tomorrow') {
      this.handleTomorrowQuery();
    }
  },
  watch: {
    // 监听路由变化，处理参数变化
    '$route.query': {
      handler(query) {
        if (query.type === 'today') {
          this.handleTodayQuery();
        } else if (query.type === 'tomorrow') {
          this.handleTomorrowQuery();
        }
      },
      immediate: false
    }
  },
  methods: {
    /** 查询行程列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeArrivalTime && '' != this.daterangeArrivalTime) {
        this.queryParams.params["beginArrivalTime"] = this.daterangeArrivalTime[0] + " 00:00:00";
        this.queryParams.params["endArrivalTime"] = this.daterangeArrivalTime[1] + " 23:59:59";
      }
      listBooking(this.queryParams).then(response => {
        this.bookingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取车站列表
    getStationList() {
      listStation().then(response => {
        this.stationList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: null,
        bookingTime: null,
        arrivalTime: null,
        stationId: null,
        phone: null,
        userPhone: null,
        personCount: null,
        stationName: null,
        username: null,
        roomNo: null,
        carDesc: null,
        driverName: null,
        driverPhone: null,
        confirmTime: null,
        departureTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 今日行程按钮操作 */
    handleTodayQuery() {
      this.queryParams.pageNum = 1;
      //获取今日时间 yyyy-MM-dd 字符串
      let today = new Date();
      let start = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate();
      //获取明天日期 yyyy-MM-dd 字符串
      // let end = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
      // end = end.getFullYear() + '-' + (end.getMonth() + 1) + '-' + end.getDate();
      this.daterangeArrivalTime = [start, start];
      this.getList();
    },
    handleTomorrowQuery(){
      this.queryParams.pageNum = 1;
      //获取今日时间 yyyy-MM-dd 字符串
      let today = new Date();
      let tomorrow = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 1);
      let start = tomorrow.getFullYear() + '-' + (tomorrow.getMonth() + 1) + '-' + tomorrow.getDate();
      //获取明天日期 yyyy-MM-dd 字符串
      let end = new Date(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate() + 1);
      end = end.getFullYear() + '-' + (end.getMonth() + 1) + '-' + end.getDate();
      this.daterangeArrivalTime = [start, start];
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeArrivalTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加行程";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBooking(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改行程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBooking(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBooking(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除行程编号为"' + ids + '"的数据项？').then(function () {
        return delBooking(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotel/booking/export', {
        ...this.queryParams
      }, `booking_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
